package cn.hdu.liang.core.controller.admin;


import cn.hdu.liang.common.web.ResponseUtils;
import cn.hdu.liang.core.web.Constants;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import net.fckeditor.response.UploadResponse;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class UploadController {

    //上传图片
    @RequestMapping(value = "/upload/uploadPic.do")
    public void uploadPic(@RequestParam(required = false) MultipartFile pic, HttpServletResponse resp) {

        //获取扩展名
        String ext = FilenameUtils.getExtension(pic.getOriginalFilename());

        //生成一个随机文件名
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String genFileName = df.format(new Date());
        Random r = new Random();
        genFileName += String.valueOf(r.nextInt(900) + 100);

        //实例化Jersey
        Client client = new Client();
        //服务器路径
        String path = "upload/" + genFileName + "." + ext;
        String url = Constants.IMAGE_URL + path;
        //设置请求路径
        WebResource resource = client.resource(url);

        //发送
        try {
            resource.put(String.class, pic.getBytes());
        } catch(IOException e) {
            e.printStackTrace();
        }

        //返回两个路径
        JSONObject jo = new JSONObject();
        jo.put("url", url);
        jo.put("path", path);
        //发送
        ResponseUtils.renderJson(resp, jo.toString());

    }

    //上传Fck图片
    @RequestMapping(value = "/upload/uploadFck.do")
    public void uploadFck(HttpServletRequest req, HttpServletResponse resp) {
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) req;
        Map<String, MultipartFile> fileMap = mr.getFileMap();
        Set<Map.Entry<String, MultipartFile>> entrySet = fileMap.entrySet();
        entrySet.forEach(Map.Entry::getValue);
        for(Map.Entry<String, MultipartFile> entry : entrySet) {
            MultipartFile pic = entry.getValue();

            //获取扩展名
            String ext = FilenameUtils.getExtension(pic.getOriginalFilename());

            //生成一个随机文件名
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String genFileName = df.format(new Date());
            Random r = new Random();
            genFileName += String.valueOf(r.nextInt(900) + 100);

            //实例化Jersey
            Client client = new Client();
            //服务器路径
            String path = "upload/" + genFileName + "." + ext;
            String url = Constants.IMAGE_URL + path;
            //设置请求路径
            WebResource resource = client.resource(url);

            //发送
            try {
                resource.put(String.class, pic.getBytes());
            } catch(IOException e) {
                e.printStackTrace();
            }

            //返回URL给Fck
            UploadResponse ok = UploadResponse.getOK(url);
            try {
                resp.getWriter().print(ok);
            } catch(IOException e) {
                e.printStackTrace();
            }

        }
    }
}
